import {Component, OnInit, Input} from '@angular/core';
import { ProblemRelatedService} from './problem-related.service';
import { Response} from '@angular/http';
import { ProblemCategory} from './model/problem-model';
import { User} from "../user/model/user-model";
import {GlobalService} from "../global.service";

@Component({
  selector: 'app-problem-sets',
  templateUrl: './problem-sets.component.html',
  styleUrls: ['./problem-sets.component.css'],
})
export class ProblemSetsComponent implements OnInit {

  problemCategory:ProblemCategory = new ProblemCategory();
  problemSets:ProblemSet[];
  data:Object;
  tabHeader:string = '默认';
  display: boolean = false;//控制显示dialog
  category:any;
  buttonFlag:boolean = true;
  nowTab:number = 0;
  showTabFlag:boolean = true;
  currentUser:User;

  constructor(
    private problemRelatedService:ProblemRelatedService,
    private globalService:GlobalService,
  ) { }

  ngOnInit() {
    this.getCategory();
    this.getProblemSetList();
    console.log("获取的值是:");
    this.currentUser = this.globalService.getCurrentUser();
    console.log(this.currentUser);

    /**
     * 关闭题目集展开子菜单
     */
    console.log("li:",document.getElementById("li"));
    let li = document.getElementById("li");
    li.setAttribute('class','');
  }

  getCategory(){
    let category = {};
    this.problemRelatedService.getProblemSetsList(category)
      .subscribe((res:Response)=>{
        let data = res.json();
        if (data.success){
          console.log("categoryList:",data.rows);
          let categoryList = data.rows;
          for (let i=0;i<categoryList.length;i++){
            if (categoryList[i].name == '默认'){
              this.category = categoryList[i].code;
            }
          }
        }
      })
  }

  getProblemSetList(){
    /*let problemSets = {problemSets:[{type:'1'}]};*/
    let queryData = new QueryData();
    queryData.properties.problemSetCategoryEntity_name_obj_ae = '默认';
    if(this.globalService.getCurrentUser()&&this.globalService.getCurrentUser().problemSetCodes){//学生才会有 用户组 才会有code_in
      queryData.properties.code_in = this.globalService.getCurrentUser().problemSetCodes;
      queryData.properties.exam = this.globalService.getCurrentUser().exam;
    }else if(this.globalService.getCurrentUser()){//老师 直接用户 creator
      queryData.properties.creator = this.globalService.getCurrentUser().code;
    }
    if(!this.globalService.getCurrentUser()){
      queryData.properties.visitor = true;
    }else{
      queryData.properties.role = this.globalService.getCurrentUser().role;
    }

    let problemCategory;
    console.log("queryData:",queryData);
    this.problemRelatedService.findProblemSetInfos(queryData)
      .subscribe((res:Response)=>{
        let response = res.json();
        console.log("findProblemSetInfos:",response);
        let temp = [];
        if(response.success){
          response.rows.forEach(function (problemSet) {
            temp.push({code:problemSet.code,title:problemSet.name,creator:problemSet.creator,company:problemSet.company,type:problemSet.typeName,state:problemSet.statusName})
          });
        }
        this.problemSets = temp;
      });
  }

  onTabChange(event){
    console.log("onTabChange");
    console.log(event.index);
    let index = event.index;
    if (index == 1){
      this.nowTab = 0;

      this.showDialog();
    }
  }

  showDialog() {
    this.display = true;
  }
  hideDialog(event){
    console.log(event);
    this.nowTab = 0;
    this.showTabFlag = true;
    this.ngOnInit();
  }

  createCategory(){
    console.log("createCategory：");
    console.log(this.category);
    let problemSetCategoryInfo = {name:this.category};
    this.problemRelatedService.createCategory(problemSetCategoryInfo)
      .subscribe((res:Response)=>{
        if (res.json().success){
          console.log("创建成功");
          this.display = false;
        }
      });
  }

  changeInput(value){
    if (value!=null||value!=''){
      this.buttonFlag = false;
    }else {
      this.buttonFlag = true;
    }
  }

  /**
   * 分页触发方法
   */
  paginate(event){
    /**
     * event中的page代表当前页数，从0开始计数
     */
    console.log(event);
  }

}



export class ProblemSet{
  code;
  title;
  creator;
  company;
  type;
  state;
}
class QueryData{
  properties:Properties = new Properties;
}
class Properties{
  problemSetCategoryEntity_name_obj_ae:string;
  code_in:string;
  visitor:boolean;
  creator:string;
  exam:boolean;
  role:string;
}


